import React from 'react';
import {Form,Input,Button,message} from "antd";
import { LockOutlined, UserOutlined } from '@ant-design/icons';
import axios from "axios";
import styles from "./index.module.less";
import {useNavigate} from "react-router-dom";
function Login() {
    const navigate = useNavigate();
    return (
        <div className={styles.login}>
            <div>
                <h1>尚医通管理系统</h1>
                <Form
                    name="normal_login"
                    className="login-form"
                    onFinish={async (values:any)=>{
                        // 当用户点击提交，且通过验证以后会执行该函数。
                        // console.log("onFinish",values);
                        const {data} = await axios.post("/auth/index/login",values);
                        if(data.code === 200){
                            message.success(data.message);
                            // 保存token
                            localStorage.setItem("token",data.data.token);
                            // 跳转至首页
                            navigate("/");

                        }else{
                            message.error(data.message);
                        }
                    }}
                >
                    <Form.Item
                        name="username"
                        rules={[{ required: true, message: '请输入管理员账号' }]}
                    >
                        <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="请输入管理员账号" />
                    </Form.Item>
                    <Form.Item
                        name="password"
                        rules={[{ required: true, message: '请输入管理员密码！' }]}
                    >
                        <Input
                            prefix={<LockOutlined className="site-form-item-icon" />}
                            type="password"
                            placeholder="请输入管理员密码"
                        />
                    </Form.Item>

                    <Form.Item>
                        <Button type="primary" htmlType="submit" className="login-form-button">
                            登陆
                        </Button>
                    </Form.Item>
                </Form>
            </div>
        </div>
    );
}

export default Login;